# -*- coding=utf-8 -*-

#单页数据来源：https://db.yaozh.com/hmap?grade=全部&p=1&pageSize=30&province=辽宁省&type=全部
#能在第一页获取到数据总数
#最大单页为30条记录
#单击事件链接补全：https://db.yaozh.com/+/hmap/14.html

import re
import os
import os.path
import urllib.request

from bs4 import BeautifulSoup
from demo_analysis_website.hospitalObj import hospitalObj



def request_web_source_body(url):
    regex_rule='<div class="offset-top table-list" data-max="300">([\s\S]*?)</div>'
    html_body_opener=urllib.request.urlopen(url).read()
    html_body_source=html_body_opener.decode("utf-8")
    #print(html_body_source)

    return get_regex_msg(html_body_source,regex_rule)

def get_page_obj(content):
    hospitallist=list()
    soup = BeautifulSoup(content, 'html.parser')
    # print(soup)
    trs = soup.find_all(name="tr")
    for tr in trs:
        print(tr)
        print("################################开始解析##########################################")
        _soup = BeautifulSoup(str(tr), 'html.parser')
        print(_soup)
        print("################################开始解析name##########################################")
        ths=_soup.find_all(name="th")
        if (len(ths) != 0):
            print(ths[0].getText())

        print("********************************解析name结束,开始解析body******************************************")
        tds = _soup.find_all(name="td")
        if (len(tds) != 0):
            print(tds[0].getText())
        print("********************************解析name结束,解析body结束******************************************")

            #print(_soup)
        # ths = _soup.find_all(name="th")
        #
        # if (len(ths) != 0):
        #     hospitalobj = hospitalObj()
        #     hospitalobj.setName(ths[0].getText())
        #     hospitalobj.set(ths[1].getText())
        #     hospitalobj.setBankName(ths[2].getText())
        #     hospitalobj.setBankTel(ths[3].getText())
        #     hospitalobj.setBankAddr(ths[4].getText())
        #     hospitallist.append(hospitalobj)
        # else:
        #     continue

    #return hospitallist



def get_regex_msg(str,regex_pattern):
    return re.compile(regex_pattern).findall(str)


if __name__ == '__main__':
    base_url = "https://db.yaozh.com/hmap"
    province_name="四川省"
    require_param="?grade=全部&p=1&pageSize=30&province="+province_name+"&type=全部"
    request_web_page_source= request_web_source_body(base_url)[0]
    #print(request_web_page_source)
    get_page_obj(request_web_page_source)

